﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;

namespace EncorePlugin
{
    public class EncorePlugin : GUIWindow, ISetupForm
    {
        [SkinControlAttribute(50)]
        //protected GUIListControl list = null;
        public GUIFacadeControl facade = null;

        [SkinControlAttribute(7)]
        protected GUIButtonControl searchButton = null;

        public EncorePlugin()
        {

        }

        #region ISetupForm Members

        // Returns the name of the plugin which is shown in the plugin menu
        public string PluginName()
        {
            return "Encore!";
        }

        // Returns the description of the plugin is shown in the plugin menu
        public string Description()
        {
            return "Encore!";
        }

        // Returns the author of the plugin which is shown in the plugin menu
        public string Author()
        {
            return "adamanth";
        }

        // show the setup dialog
        public void ShowPlugin()
        {
            MessageBox.Show("Nothing to configure");
        }

        // Indicates whether plugin can be enabled/disabled
        public bool CanEnable()
        {
            return true;
        }

        // Get Windows-ID
        public int GetWindowId()
        {
            // WindowID of windowplugin belonging to this setup
            // enter your own unique code
            return 5678;
        }

        // Indicates if plugin is enabled by default;
        public bool DefaultEnabled()
        {
            return true;
        }

        // indicates if a plugin has it's own setup screen
        public bool HasSetup()
        {
            return true;
        }

        /// <summary>
        /// If the plugin should have it's own button on the main menu of MediaPortal then it
        /// should return true to this method, otherwise if it should not be on home
        /// it should return false
        /// </summary>
        /// <param name="strButtonText">text the button should have</param>
        /// <param name="strButtonImage">image for the button, or empty for default</param>
        /// <param name="strButtonImageFocus">image for the button, or empty for default</param>
        /// <param name="strPictureImage">subpicture for the button or empty for none</param>
        /// <returns>true : plugin needs it's own button on home
        /// false : plugin does not need it's own button on home</returns>

        public bool GetHome(out string strButtonText, out string strButtonImage,
          out string strButtonImageFocus, out string strPictureImage)
        {
            strButtonText = PluginName();
            strButtonImage = String.Empty;
            strButtonImageFocus = String.Empty;
            strPictureImage = String.Empty;
            return true;
        }

        // With GetID it will be an window-plugin / otherwise a process-plugin
        // Enter the id number here again
        public override int GetID
        {
            get
            {
                return 5678;
            }

            set
            {
            }
        }

        #endregion

        public override bool Init()
        {
            return Load(GUIGraphicsContext.Skin + @"\Encore.xml");
        }

        protected override void OnPageLoad()
        {
            facade.View = GUIFacadeControl.ViewMode.List;

            EncoreService service = EncoreService.GetInstance();
            foreach (EncorePlaylistItem playListItem in service.GetPlaylist())
            {
                GUIListItem item = new GUIListItem();
                item.ItemId = playListItem.Id;
                item.Label = string.Format("{0}: {1}", playListItem.Id, playListItem.Name);
                item.MusicTag = playListItem;

                item.OnItemSelected += new GUIListItem.ItemSelectedHandler(SongSelected);

                facade.Add(item);
            }

            GUIPropertyManager.SetProperty("#selectedSong", string.Empty);
        }

        protected override void OnClicked(int controlId, GUIControl control,
              MediaPortal.GUI.Library.Action.ActionType actionType)
        {
            if (control == searchButton)
                OnSearchButton();
            if (control == facade)
                PlaySong(facade.SelectedListItem);
            base.OnClicked(controlId, control, actionType);
        }

        private void OnSearchButton()
        {
            string searchString = string.Empty;
            if (GetUserInputString(ref searchString, false))
            {
                GUIListItem foundItem = facade.ListView.ListItems.Find(delegate(GUIListItem item)
                {
                    try
                    {
                        return item.ItemId == int.Parse(searchString);
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                });

                if (foundItem != null)
                {
                    PlaySong(foundItem);
                }
            }
        }

        protected void SongSelected(GUIListItem item, GUIControl parent)
        {
            EncorePlaylistItem playlistItem = item.MusicTag as EncorePlaylistItem;
            GUIPropertyManager.SetProperty("#selectedSong", string.Format("{0} - {1}", playlistItem.Artist, playlistItem.Name));
        }

        private bool GetUserInputString(ref string sString, bool password)
        {
            VirtualKeyboard keyBoard = (VirtualKeyboard)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_VIRTUAL_KEYBOARD);
            keyBoard.Reset();
            keyBoard.IsSearchKeyboard = true;
            keyBoard.Text = sString;
            keyBoard.Password = password;
            keyBoard.DoModal(GetID); // show it...
            if (keyBoard.IsConfirmed) sString = keyBoard.Text;
            return keyBoard.IsConfirmed;
        }

        protected void PlaySong(GUIListItem item)
        {
            GUIDialogYesNo dlg = (GUIDialogYesNo)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_YES_NO);
            dlg.SetHeading("Play song");
            dlg.SetLine(1, String.Format("Do you realy want to play song #{0} ({1})?", item.ItemId, item.Label));
            dlg.SetLine(2, String.Empty);
            dlg.SetLine(3, String.Empty);
            dlg.DoModal(GUIWindowManager.ActiveWindow);

            if (!dlg.IsConfirmed)
            {
                return;
            }

            //...
        }
    }
}
